﻿package bdz.projects.slab.data {
	import bdz.common.model.AppEnvironment;
	import bdz.dms.core.DMSConfig;
	import bdz.dms.core.DMSGateway;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.events.DMSEvent;
	import bdz.dms.vo.ContentVO;
	import bdz.dms.vo.SectionVO;
	import bdz.dms.vo.UserVO;
	import bdz.projects.slab.events.SlabSiteEvent;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	/**
	* ...
	* @author Default
	*/
	public class SlabSiteData extends EventDispatcher {
		
		private var gateway:DMSGateway;
		private var config:DMSConfig
		private var env:AppEnvironment = AppEnvironment.getEnv();
		private var _publicgallery:DMSCollection;
		private var _artists:DMSCollection;
		private var _siteinfo:SectionVO;
		private var _news:DMSCollection;
		private var _privategallery:DMSCollection;
		private var _galleryinfo:Dictionary = new Dictionary();
		private var _pgalleryinfo:Dictionary = new Dictionary();
		private static var instance:SlabSiteData;
		private var preloading:Boolean = true;
		private var _activeUser:UserVO;
		
		function SlabSiteData(paccess:PrivateAccess):void {
			if(paccess == null){
				throw new Error("NullPointerException...");
			}
		}
		public static function getInstance(cfg:DMSConfig = null):SlabSiteData {
			if (instance == null) {
				instance = new SlabSiteData(new PrivateAccess());
				instance.initialize(cfg);
			}
			return instance;
		}
		private function initialize(cfg:DMSConfig):void {
			gateway = DMSGateway.getInstance(cfg);
			gateway.addEventListener(DMSEvent.LOGIN_SUCCESSFUL, onLoginSuccessful);
			gateway.addEventListener(DMSEvent.LOGIN_FAILED, onLoginFailed);
		}
		private function privateGalleryLoaded(evt:DMSEvent):void {
			if (evt.data is SectionVO) {
				_privategallery = new DMSCollection();
				_privategallery.push(evt.data);
			}else {
				_privategallery = evt.data;
			}
			if (_privategallery == null) {
				_privategallery = new DMSCollection();
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.PRIVATE_GALLERY_LOADED, _privategallery));
		}
		private function publicGalleryLoaded(evt:DMSEvent):void {
			if (evt.data is SectionVO) {
				_publicgallery = new DMSCollection();
				_publicgallery.push(evt.data);
			}else {
				_publicgallery = evt.data;
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.PUBLIC_GALLERY_LOADED, _publicgallery));
		}
		private function siteInfoLoaded(evt:DMSEvent):void {
			if (evt.data is SectionVO) {
				_siteinfo = evt.data as SectionVO;
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.SITE_INFO_LOADED, _siteinfo));
		}
		private function artistsLoaded(evt:DMSEvent):void {
			if (evt.data is SectionVO) {
				_artists = new DMSCollection();
				_artists.push(evt.data);
			}else {
				_artists = evt.data;
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.ARTISTS_LOADED, _artists));
		}
		private function pgalleryInfoLoaded(evt:DMSEvent):void {
			
			var gi:*;
			if (evt.data is DMSCollection) {
				gi = _pgalleryinfo[evt.data[0].section_id] = evt.data;
			}else {
				var dcoll:DMSCollection = new DMSCollection();
				dcoll.push(evt.data);
				gi = _pgalleryinfo[evt.data.section_id] = dcoll;
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.PRIVATE_GALLERY_INFO_LOADED, gi));
			
		}
		private function galleryInfoLoaded(evt:DMSEvent):void {
			var gi:*;
			if (evt.data is DMSCollection) {
				gi = _galleryinfo[(evt.data is ContentVO) ? evt.data[0].section_id : evt.data[0].parent_id] = evt.data;
			}else {
				var dcoll:DMSCollection = new DMSCollection();
				dcoll.push(evt.data);
				gi = _galleryinfo[(evt.data is ContentVO) ? evt.data.section_id : evt.data.parent_id] = dcoll;
			}
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.PUBLIC_GALLERY_INFO_LOADED, gi));
		}
		private function onLoginSuccessful(evt:DMSEvent):void {
			_activeUser = evt.data as UserVO;
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.LOGIN_SUCCESSFUL));
		}
		private function onLoginFailed(evt:DMSEvent):void {
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.LOGIN_FAILED));
		}
		public function login(uname:String, pword:String):void {
			gateway.login(uname, pword);
		}
		public function getSiteInfo():SectionVO{
			if (_siteinfo) {
				return _siteinfo;
			}else { 
				gateway.getSectionAndContent(env.getSetting("siteinfo_id"), siteInfoLoaded);
			}
			return null;
		}
		public function getArtistList():DMSCollection {
			if (_artists) {
				return _artists;
			}else {
				gateway.getSectionAndContent(env.getSetting("artists_id"), artistsLoaded);
			}
			return null;
		}
		public function sendInquiry(subj:String, msg:String, from:String, responder:Function):void {
			gateway.sendGenericMail("slab@silverlensphoto.com", "SLAB INQUIRY - " + subj, msg, from, responder);
			//gateway.sendGenericMail("badongers@gmail.com", "SLAB INQUIRY - " + subj, msg, from, responder);
		}
		public function getPublicGalleries():DMSCollection {
			if (_publicgallery) {
				return _publicgallery;
			}else {
				gateway.getSectionAndContent(env.getSetting("gallery_id"), publicGalleryLoaded);
			}
			return null;
		}
		public function cleanPrivateGalleries():void {
			_privategallery = null;
		}
		public function getPrivateGalleries():DMSCollection {
			if (_privategallery) {
				return _privategallery;
			}else {
				gateway.getSectionAndContent(env.getSetting("private_id"), privateGalleryLoaded);
			}
			return null;
		}
		public function getPrivateGalleryInfo(id:Number):DMSCollection {
			if (_pgalleryinfo[id]) {
				return _pgalleryinfo[id];
			}else {
				gateway.getSectionAndContent(id, pgalleryInfoLoaded);
			}
			return null;
		}
		public function getGalleryInfo(id:Number):DMSCollection {
			if (_galleryinfo[id]) {
				return _galleryinfo[id];
			}else {
				gateway.getSectionAndContent(id, galleryInfoLoaded);
			}
			return null;
		}
		public function get loggedInUser():UserVO {
			return _activeUser;
		}
	}
	
}
/**
 * @private
 * 
 */
class PrivateAccess{}